package com.mo.lib.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.dylanc.longan.activity
import com.gyf.immersionbar.ktx.immersionBar
import com.mo.lib.ui.action.HandlerAction
import com.mo.lib.ui.dialog.LoadingDialogCar

/**
 * @ author：mo
 * @ data：2022/3/21:15:40
 * @ 功能：ViewDataBinding基类
 * 注意：
 */

abstract class BaseActivity(@LayoutRes layoutResId: Int = 0) : AppCompatActivity(layoutResId), HandlerAction {
//     val loadingDialog by lazy {
//        LoadingDialogCar()
//    }
  private  var loadingDialogCar: LoadingDialogCar? = null

    open fun loadingShow() {
        if (loadingDialogCar == null) {
            loadingDialogCar = LoadingDialogCar()
        }
        loadingDialogCar?.show()
    }

    open fun loadingOver() {
        if (!activity.isDestroyed) {
            loadingDialogCar?.dismisss()
            loadingDialogCar = null
        }

    }

    /** 是否沉浸状态栏，默认否*/
    open fun isTranslucentStatus() = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isTranslucentStatus()) {
            immersionBar {
                statusBarDarkFont(true)
                transparentStatusBar()
            }
        }
        if (keyboardEnable()) {
            immersionBar {
                keyboardEnable(true)
            }
        }

        Looper.myQueue().addIdleHandler {
            initData()
            false
        }
        initView(savedInstanceState)
    }

    abstract fun initView(savedInstanceState: Bundle?)

    abstract fun initData()

    /** 点击监听*/
    open fun onClickView(view: View) {
    }

    /**跳转界面时隐藏软键盘 */
    override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) {
        hideSoftKeyboard() // 查看源码得知 startActivity 最终也会调用 startActivityForResult
        super.startActivityForResult(intent, requestCode, options) //        overridePendingTransition(R.anim.right_in_activity, R.anim.right_out_activity)
    }

    /**重写关闭界面方法 */
    override fun finish() { //      if (  loadingDialog.dialog.isShowing){
        //          loadingDialog.dismisss()
        //      }

        hideSoftKeyboard()
        super.finish() //        overridePendingTransition(R.anim.left_in_activity, R.anim.left_out_activity)
    }

    override fun onDestroy() {
//        loadingDialog.dismisss()
        loadingOver()
        super.onDestroy()
    }

    /**
     * 隐藏软键盘
     */
    private fun hideSoftKeyboard() { // 隐藏软键盘，避免软键盘引发的内存泄露
        val view = currentFocus
        view?.run {
            val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
            manager?.hideSoftInputFromWindow(this.applicationWindowToken, 0) //applicationWindowToken可以隐藏整个app的键盘,而使用windowToken的时候隐藏不了Fragment的键盘
            //manager?.hideSoftInputFromWindow(this.windowToken, 0)
        }
    }

    /**页面特殊销毁的话通过该方法重新赋值 */
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
    }

    /** 键盘模式是否开启     使用沉浸式解决键盘覆盖输入框的问题*/
    open fun keyboardEnable(): Boolean {
        return false
    }
    /**
     * 重置App界面的字体大小，fontScale 值为 1 代表默认字体大小
     *
     * @return 重置继承该activity子类的文字大小，使它不受系统字体大小限制
     */ //    override fun getResources(): Resources {
    //        val res = super.getResources()
    //        val config = res.configuration
    //        var isChanged = false //如果字体大小不等于1F,则更改字体大小
    //        takeUnless { config.fontScale == 1F }?.let {
    //            config.fontScale = 1F
    //            isChanged = true
    //        } //如果本地语言不是英文,则更改语言
    //        takeUnless { config.locale == Locale.ENGLISH }?.let {
    //            config.setLocale(Locale.ENGLISH)
    //            isChanged = true
    //        } //如果资源文件发生变化,则更新
    //        takeIf { isChanged }?.let {
    //            KLog.i("更新本地资源文件")
    //            res.updateConfiguration(config, res.displayMetrics)
    //        }
    //        return res
    //    }


    /** 原viewBinding 初始化*/ //    @SuppressLint("InflateParams") override fun setContentView(layoutResId: Int) {
    //        val contentView = layoutInflater.inflate(R.layout.activity_base, null, false)
    //        setContentView(contentView)
    //        val container = contentView as ViewGroup
    //        if (getTitleLaout() != 0) {
    //            val toolbar = layoutInflater.inflate(getTitleLaout(), container, false)
    //            container.addView(toolbar)
    //            initTitleView(toolbar)
    //        }
    //        viewBinding = DataBindingUtil.inflate(layoutInflater, layoutResId, container, true)
    //        rootView = viewBinding.root
    //        initView()
    //    }
}